package com.tdam2011.grupo04;

import com.tdam2011.grupo04.entidades.Contacto;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.Filter;

public class MasterListActivity extends ListActivity{
	protected static final String KEY_CONTACT_FILTER_APPLIED = "KEY_CONTACT_FILTER_APPLIED";
	protected static final String KEY_CONTACT_FILTER = "KEY_CONTACT_FILTER";
	protected static final String KEY_CONTACT_TO_SHOW = "KEY_CONTACT_TO_SHOW";
		
	protected Contacto contactToShow;
	protected String contactFilter;
	protected boolean contactFilterApplied;
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        if (savedInstanceState != null) {
			contactFilter = savedInstanceState.getString(KEY_CONTACT_FILTER);
			contactFilterApplied = savedInstanceState
					.getBoolean(KEY_CONTACT_FILTER_APPLIED);
			contactToShow = (Contacto) savedInstanceState.getSerializable(KEY_CONTACT_TO_SHOW);
		}
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putBoolean(KEY_CONTACT_FILTER_APPLIED, contactFilterApplied);
		outState.putString(KEY_CONTACT_FILTER, contactFilter);
		outState.putSerializable(KEY_CONTACT_TO_SHOW, contactToShow);
	}
    
    protected void publishFilterApplied() {
		if (contactFilter == null || contactFilter.length() == 0) {
			contactFilterApplied = false;
		} else {
			contactFilterApplied = true;
		}
	}
	
	public class ContactFilter extends Filter {

		@Override
		protected FilterResults performFiltering(CharSequence constraint) {
			String filter = constraint.toString().toLowerCase().trim();
			contactFilter = filter;
			return null;
		}

		@Override
		protected void publishResults(CharSequence constraint,
				FilterResults results) {
			publishFilterApplied();
		}
	}
}
